package com.idopartx.phonelightning.ui.permissionguide

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.dotools.umlibrary.UMPostUtils
import com.idopartx.phonelightning.MainActivity
import com.idopartx.phonelightning.R
import com.idopartx.phonelightning.common.Constant
import com.idopartx.phonelightning.utils.PermissionUtils
import com.idopartx.phonelightning.utils.PreferenceSetting
import com.tencent.mmkv.MMKV

/***华为及OPPO10~12**/
class Help1PermissionActivity : AppCompatActivity() {

    var isJumpAutoStart = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_huaweisetting)

        findViewById<Button>(R.id.start_auto_backrun_btn).setOnClickListener {
            UMPostUtils.onEvent(this, "authority_auto_allow_click")
            if (PermissionUtils.isHuaWeiDevice()) {
                HuaweiAutoStartGuideDialog(
                    this,
                    object : HuaweiAutoStartGuideDialog.DialogCallBack {
                        override fun onOk(index: Int) {
                            UMPostUtils.onEvent(
                                applicationContext,
                                "authority_auto_allow_jump"
                            )
                            PermissionUtils.startAutoStartSetting(this@Help1PermissionActivity)
                            isJumpAutoStart = true
                            updatePermissionStatus()
                        }
                    }).show()
            } else if (PermissionUtils.isOppoDevice() && Build.VERSION.SDK_INT >= 29) {
                OppoAutoStartGuideDialog(this, object : OppoAutoStartGuideDialog.DialogCallBack {
                    override fun onOk(index: Int) {
                        UMPostUtils.onEvent(
                            applicationContext,
                            "authority_auto_allow_jump"
                        )
                        PermissionUtils.startOppoPowerManager(this@Help1PermissionActivity)
                        isJumpAutoStart = true
                        updatePermissionStatus()
                    }
                }).show()
            }
        }

        findViewById<Button>(R.id.next_step).setOnClickListener {
            PreferenceSetting.setFirst(this, false)
//            MMKV.defaultMMKV().encode(Constant.MMKV_IS_GUIDE, true)
            UMPostUtils.onEvent(this, "authority_finish_click")

            val intent = Intent(this, MainActivity::class.java)
            intent.putExtra("isSplash", true)
            startActivity(intent)
            finish()
        }
    }

    fun updatePermissionStatus() {
        if (isJumpAutoStart) {
            findViewById<Button>(R.id.start_auto_backrun_btn).setBackgroundResource(R.drawable.shape_solid_42d174_radius_4)
            findViewById<Button>(R.id.start_auto_backrun_btn).text = "已开启"
        } else {
            findViewById<Button>(R.id.start_auto_backrun_btn).setBackgroundResource(R.drawable.shape_solid_3ac8bf_radius_4)
        }
    }

    override fun onResume() {
        super.onResume()
        updatePermissionStatus()
    }
}